package diaporama;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class DiaporamaView extends JFrame{

	private int iframe = 0;
	private JFrame frame = null;
	private JPanel panel = null;
	private JLabel lab = null;
	private ArrayList<String> toto = null;
	
	public DiaporamaView(ArrayList<String> list)
	{
		ArrayList<String> toto = list;
		long delay = 2500L;
		frame = new JFrame();
		panel = new JPanel();
		JPanel panBoutton = new JPanel();
		frame.setTitle("Diaporama");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.add(panel, BorderLayout.CENTER);
		//ajouter 
		panBoutton.setLayout(new GridLayout(1,1));
		JButton fermer = new JButton("fermer fenetre");
		fermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		panBoutton.add(fermer);
		//fin ajouter
		// frame.setLayout(new GridLayout(2, 1));
		this.frame.setVisible(true);
		this.panel.setBackground(Color.GRAY);
		frame.setLocation(100, 200);
		try {
			while (iframe < toto.size()) {
				if (iframe == 0)
				frame.setSize(400, 450);
				else if (iframe % 2 == 0)
					frame.setSize(401, 451);
				else
					frame.setSize(399, 449);
				Image img = ImageIO.read(new File(toto.get(iframe)));
//				redimensionner les images, a faire 
//				Image scaleImg = img.getScaledInstance(150, -1, Image.SCALE_FAST);
				ImageIcon ii = new ImageIcon(img);
				//fin redim
				lab = new JLabel(ii);
				panel.add(lab);
				frame.add(panBoutton, BorderLayout.SOUTH);
				//repaint();
				Thread.sleep(delay);
				panel.remove(lab);
				panel.revalidate();
				iframe++;
//				frame.pack();
				 frame.repaint();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//	public void paint(Graphics g) {
//		Image current = getToolkit().getImage(toto.get(iframe));
//		g.drawImage(current, 0, 0, this);
//	}

}
